const express = require('express')
const router = express.Router()
const path = require('path')

// 获取验证模块
const authorization = require(path.join(process.cwd(), '/modules/authorization'))

const managerService = require(path.join(process.cwd(), '/services/managerService'))
// authorization.getService('managerService')

/**
 * @api {get} /api/private/v1/users 查询用户列表
 * @apiName users
 * @apiGroup 用户管理模块
 * @apiHeader  Authrization  鉴权token
 * @apiQuery {Number} pageNum 页码
 * @apiQuery {Number} pageSize 分页大小
 * @apiQuery {String} condition 查询关键字
 * @apiSuccessExample {type} Success-Response
 *    HTTP/1.1 200 OK
 *    {
 *      "data": {
 *        "total": 1,
 *        "list": [
 *           {
 *                  id: 01323dsds,
 *					role_name: 超级管理员,
 *					username: admin,
 *	    			create_time: 2020-12-04 00:00:02,
 *					mobile: 1589262362,
 *					email: 256565@qq.com,
 *					status: true
 *           }
 *              ]
 *      },
 *      "status": 200,
 *      "message": "用户列表获取成功"
 *    }
 * @apiSampleRequest http://localhost:8888/api/private/v1/users
*/
router.get('/', (req, res) => {
    if (!req.query.pageNum || req.query.pageNum <= 0) return res.sendResult(null, 400, 'pageNum参数错误')
    if (!req.query.pageSize || req.query.pageSize <= 0) return res.sendResult(null, 400, 'pageSize参数错误')
    managerService.getAllManagers({
        query: req.query.condition, // 查询关键字
        pageNum: req.query.pageNum,
        pageSize: req.query.pageSize
    }, (err, result) => {
        if (err) return res.sendResult(null, 400, err)
        res.sendResult(result, 200, '获取管理员列表成功')
    })
})

// 获取用户信息(get)
router.get('/:id', (req, res) => {
    if (!req.params.id) return res.sendResult(null, 400, '用户ID不能为空')
    if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, '用户ID必须是数字')
    managerService.getManager(req.params.id, (err, manager) => {
        if (err) res.sendResult(null, 400, err)
        res.sendResult(manager, 200, '获取成功')
    })
})

// 创建用户(post)
router.post('/', (req, res) => {
    if (!req.body.username) return res.sendResult(null, 400, '用户名不能为空')
    if (!req.body.password) return res.sendResult(null, 400, '密码不能为空')
    if (!req.body.rid) {
        req.body.rid = -1
    }
    if (isNaN(parseInt(req.body.rid))) req.body.rid = -1
    managerService.createManager({
        "username":req.body.username,
        "password":req.body.password,
        "mobile":req.body.mobile,
        "email":req.body.email,
        "rid":req.body.rid
    }, (err, manager) => {
        if(err) return res.sendResult(null, 400, err);
        res.sendResult(manager, 201,"创建成功");
    })
})

// 修改用户信息
router.put('/:id', (req, res) => {
    if (!req.params.id) return res.sendResult(null, 400, '用户ID不能为空')
    if(isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, '用户ID必须是数字')
    managerService.updateManager({
        id: req.params.id,
        mobile: req.body.mobile,
        email: req.body.email
    }, (err, manager) => {
        if (err) return res.sendResult(null, 400, err)
        res.sendResult(manager, 200, '更新成功')
    })
})

// 删除用户信息
router.delete('/:id', (req, res) => {
    if (!req.params.id) return res.sendResult(null, 400, '用户ID不能为空')
    if(isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, '用户ID必须是数字')
    if (req.params.id == 500) return res.sendResult(null, 400, '不允许删除admin账户')
    managerService.deleteManager(req.params.id, (err) => {
        if (err) return res.sendResult(null, 400, err)
        return res.sendResult(null, 200, '删除成功')
    })
})

// 分配用户角色
router.put('/:id/role', (req, res) => {
    if (!req.params.id) return res.sendResult(null, 400, '用户ID不能为空')
    if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, '用户ID必须是数字')
    if (req.params.id === 500) return res.sendResult(null, 400, '不允许修改admin账户')
    if (!req.body.rid) return res.sendResult(null, 400, '权限ID不能为空')
    managerService.setRole(req.params.id, req.body.rid, (err, manager) => {
        if (err) return res.sendResult(null, 400, err)
        return res.sendResult(manager, 200, '设置角色成功')
    })
})

// 更新用户状态
router.put('/:id/state/:state', (req, res) => {
    if (!req.params.id) return res.sendResult(null, 400, '用户ID不能为空')
    if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, '用户ID必须是数字')
    let state = 0
    if (req.params.state && req.params.state === 'true') state = 1
    managerService.updateManagerState(req.params.id, state, (err, manager) => {
        if (err) return res.sendResult(null, 400, err)
        return res.sendResult(manager, 200, '更新用户状态成功')
    })
})
module.exports = router